package com.autoTools.util;

import java.awt.Button;
import java.awt.Choice;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.TextField;
import java.util.HashMap;

import com.autoTools.view.*;;

//id="%d|563,%d|51"
public class ClassUtil
{
	private static String TAG = "ClassUtil";
	private static HashMap<String,Class<?>>markMap = new HashMap<String,Class<?>>(); 
	private static ClassUtil mClassUtil = null;
	
	
	private static Class<?> ButtonViewClass = ButtonView.class;
	private static Class<?> FrameViewClass = FrameView.class;
	private static Class<?> PanelViewClass = PanelView.class;
	private static Class<?> MenuBarViewClass = MenuBarView.class;
	private static Class<?> MenuViewClass = MenuView.class;
	private static Class<?> ChoiceViewClass = ChoiceView.class;
	private static Class<?> LabelViewClass = LabelView.class;
	private static Class<?> TextFieldViewClass = TextFieldView.class;
	private static Class<?> MenuItemViewClass = MenuItemView.class;
	private static Class<?> BaseViewClass = BaseView.class;
	
	private static Class<?> FrameClass = Frame.class;
	private static Class<?> PanelClass = Panel.class;
	private static Class<?> MenuClass = Menu.class;
	private static Class<?> ButtonClass = Button.class;
	private static Class<?> ChoiceClass = Choice.class;
	private static Class<?> LabelClass = Label.class;
	private static Class<?> TextFieldClass = TextField.class;
	private static Class<?> MenuBarClass = MenuBar.class;
	private static Class<?> MenuItemClass = MenuItem.class;
	private static Class<?> ComponentClass = Component.class;
	
	private static Class<?> intClass = int.class;
	private static Class<?> StringClass = String.class;
	private static Class<?> floatClass = float.class;
	private static Class<?> doubleClass = double.class;
	private static Class<?> boolClass = boolean.class;
	private static Class<?> ObjectClass = Object.class;
	
	public static ClassUtil getInstance(){
		if(mClassUtil == null){
			mClassUtil = new ClassUtil();
		}
		return mClassUtil;
	}
	
	public ClassUtil(){
		markMap.put("d", int.class);
		markMap.put("s", String.class);
		markMap.put("f", float.class);
		markMap.put("b", byte.class);
		markMap.put("bool", boolean.class);

		markMap.put("int", int.class);
		markMap.put("string", String.class);
		markMap.put("float", float.class);
		markMap.put("double", double.class);
		markMap.put("byte", byte.class);
		markMap.put("boolean", boolean.class);
		
	}
	
	public static ReflectParam getReflectParam(String param){
		ReflectParam reflectParam = null;
		if(param != null){
			String[] paramStrs = param.split(",");
			int classIndex1,classIndex2,paramStartIndex;
			String sub1,sub2;
			String item = null;
			Class<?>[] clas = new Class<?>[paramStrs.length];
			Object[] Objs = new Object[paramStrs.length];
			for(int i=0;i<paramStrs.length;i++){
				if(paramStrs[i] != null){
					
					
					item = paramStrs[i];
					//class
					classIndex1 = item.indexOf("%")+1;
					classIndex2 = item.indexOf("|");
					sub1 = item.substring(classIndex1, classIndex2).trim();
					clas[i] = getClass(sub1);
					//param
					paramStartIndex = item.indexOf("|")+1;
					sub2 = item.substring(paramStartIndex).trim();
					Objs[i] = getParam(sub2,clas[i]);
				}
			}
			reflectParam = new ReflectParam(clas,Objs);
		}
		return reflectParam;
	}
	
	public static Class<?> getClass(String param){
		Class<?> clas = null;
		if(param != null){
			clas = markMap.get(param);
			if(clas != null){
				LogUtil.sysLog(TAG,"getClass="+clas.toString());
			}else{
				LogUtil.sysLog(TAG, "getClass failed");
			}
		}
		return clas;
	}
	
	public static Object getParam(String param,Class<?> type){
		Object obj = null;
		if(param != null && type != null){
			if(type.equals(int.class)){
				obj = Integer.valueOf(param);
			}else if(type.equals(String.class)){
				obj = param;
			}else if(type.equals(float.class)){
				obj = Float.valueOf(param);
			}else if(type.equals(double.class)){
				obj = Double.valueOf(param);
			}else if(type.equals(byte.class)){
				obj = Byte.valueOf(param);
			}else if(type.equals(boolean.class)){
				if(param.equalsIgnoreCase("true")){
					obj = true;
				}else if(param.equalsIgnoreCase("false")){
					obj = false;
				}
			}else{
				LogUtil.sysLog(TAG, "Can't found this param class");
				LogUtil.sysLog("Class<?> type="+type.toString());
			}
		}
		return obj;
	}
	
}
